package dao.impl;

import dao.GoodsDao;
import pojo.entity.Goods;
import util.GetResultSet;
import util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public List<Goods> selectAll(Map<String, Object> map) {
        List<Goods> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select * from goods where 1=1 ");
        Object goodsName = map.get("goodsName");
        Object shopName = map.get("shopName");
        Object startTime = map.get("startTime");
        Object goodsStatus = map.get("goodsStatus");

        if(goodsName != null && !goodsName.toString().isEmpty()){
            sql.append(" and goodsName like ? ");
            params.add("%"+goodsName+"%");
        }
        if(shopName != null && !shopName.toString().isEmpty()){
            sql.append(" and goodsShopName like ? ");
            params.add("%"+shopName+"%");
        }
        if(startTime != null && !startTime.toString().isEmpty()){
            sql.append(" and addTime between ?  and  ? ");
            params.add(startTime.toString());
            params.add(map.get("endTime").toString());
        }
        if(goodsStatus != null && !goodsStatus.toString().isEmpty()){
            sql.append(" and goodsStatus = ? ");
            params.add(Integer.parseInt(goodsStatus.toString()));
        }

        sql.append(" limit ?,? ");
        int pn = Integer.parseInt(map.get("pageNum").toString());
        int ps = Integer.parseInt(map.get("pageSize").toString());
        params.add((pn-1)*ps);
        params.add(ps);
        System.out.println("goods的列表查询语句："+sql);
        System.out.println("填充数据"+Arrays.toString(params.toArray()));

        ResultSet set = JDBCDruidUtil.query(sql.toString(), params.toArray());
        if(set != null){
            try{
                while(set.next()){
                    GetResultSet<Goods> get = new GetResultSet<>();
                    Goods goods = get.get(Goods.class, set);
                    list.add(goods);
                }
                return list;
            }catch(SQLException e){
                e.printStackTrace();
            }finally {
                JDBCDruidUtil.close();
            }
        }
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select count(*) from goods where 1=1 ");
        Object goodsName = map.get("goodsName");
        Object shopName = map.get("shopName");
        Object startTime = map.get("startTime");
        Object goodsStatus = map.get("goodsStatus");

        if(goodsName != null && !goodsName.toString().isEmpty()){
            sql.append(" and goodsName like ? ");
            params.add("%"+goodsName+"%");
        }
        if(shopName != null && !shopName.toString().isEmpty()){
            sql.append(" and goodsShopName like ? ");
            params.add("%"+shopName+"%");
        }
        if(startTime != null && !startTime.toString().isEmpty()){
            sql.append(" and addTime between ?  and  ? ");
            params.add(startTime.toString());
            params.add(map.get("endTime").toString());
        }
        if(goodsStatus != null && !goodsStatus.toString().isEmpty()){
            sql.append(" and goodsStatus = ? ");
            params.add(Integer.parseInt(goodsStatus.toString()));
        }
        System.out.println("goods的查询数量语句："+sql);
        System.out.println("填充数据："+Arrays.toString(params.toArray()));

        ResultSet set = JDBCDruidUtil.query(sql.toString(), params.toArray());
        if(set != null){
            try{
                while(set.next()){
                    return set.getInt(1);
                }
            }catch(SQLException e){
                e.printStackTrace();
            }finally {
                JDBCDruidUtil.close();
            }
        }
        return 0;
    }

    @Override
    public int insert(Goods goods) {
        return 0;
    }

    @Override
    public int update(Goods goods) {
        return 0;
    }

    @Override
    public boolean modify(Goods goods) {
        String sql = "update goods set goodsStatus = ? where goodsId = ?  ";
        Object[] params = new Object[]{goods.getGoodsStatus(),goods.getGoodsId()};
        int i = JDBCDruidUtil.update(sql, params);
        JDBCDruidUtil.close();
        System.out.println("goods的查询数量语句："+sql);
        System.out.println("填充数据："+ Arrays.toString(params));
        return i > 0;
    }

    @Override
    public int delete(Goods goods) {
        return 0;
    }
}
